package com.oversea.action.major;

import java.util.Date;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.oversea.action.BaseAction;
import com.oversea.annotation.Permission;
import com.oversea.models.Course;
import com.oversea.models.Major;
import com.oversea.models.Majortype;
import com.oversea.service.CourseService;
import com.oversea.service.MajorService;
import com.oversea.service.MajortypeService;
import com.oversea.util.BeanUtils;
import com.oversea.util.page.Page;
import com.oversea.util.page.PageUtil;
import com.oversea.util.searchcontext.SearchContext;
import com.oversea.util.searchcontext.SearchOption;

import static com.oversea.util.ParamUtil.*;

@ParentPackage("oversea-default")
@InterceptorRefs( { @InterceptorRef("defaultStack") })
@Namespace("/majors")
@Permission(level = 3)
public class MajorAction extends BaseAction {

	private static final long serialVersionUID = -8199478948374178740L;

//	private MajorServiceImpl majorService;
	private MajorService majorService;
	
	private MajortypeService majortypeService;
	
	private CourseService courseService;

	private Major major;

	private List<Major> majorList;

	private Page page;

	private int objId;

	private List<Majortype> majortypeList;
	private List<Course> courseList;
	private List<Integer> courseIdList;

	private String majorName;
	private String majorTerms; // 学制
	private String tuitionRange; // 学费范围
	private String majorFields; // 专业领域
	private String startEnrollmentTime; // 入学时间
	private String endEnrollmentTime; // 入学时间
	private String majorDescription; // 专业描述

	@SuppressWarnings("unchecked")
	public Class getActionClass() {
		return Major.class;
	}

	private SearchContext getListSearchContext() {
		SearchContext searchContext = new SearchContext();
		if (isNotEmpty(majorName)) {
			searchContext.addOption(new SearchOption("majorName", "%"+majorName+"%",
					SearchOption.Option.like));
		}
		if (isNotEmpty(majorTerms)) {
			searchContext.addOption(new SearchOption("majorTerms", majorTerms,
					SearchOption.Option.eqaul));
		}
		if (isNotEmpty(startEnrollmentTime)) {
			searchContext.addOption(new SearchOption("enrollmentTime",
					startEnrollmentTime, SearchOption.Option.bigthanandequal));
		}
		if (isNotEmpty(endEnrollmentTime)) {
			searchContext.addOption(new SearchOption("enrollmentTime",
					endEnrollmentTime, SearchOption.Option.smallthanandeqaul));
		}
		if (isNotEmpty(majorDescription)) {
			searchContext.addOption(new SearchOption("majorDescription",
					"%"+majorDescription+"%", SearchOption.Option.like));
		}
		searchContext.setSearchClass(getActionClass());
		return searchContext;
	}

	@Action(value = "toadd", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majors/major_add.ftl") })
	public String add() {
		SearchContext searchContext = new SearchContext();
		searchContext.setSearchClass(Majortype.class);
		majortypeList = majortypeService.getObjectTotalList(searchContext);
		return SUCCESS;
	}
	
	@Action(value = "add", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String save() {
		major.setCreatedTime(new Date());
		major.setModifiedTime(new Date());
		majorService.save(major);
		return SUCCESS;
	}
	
	@Action(value = "toaddcourse", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majors/major_add_course.ftl") })
	public String addCourse() {
		major = majorService.getObjById(objId);
		SearchContext searchContext = new SearchContext();
		searchContext.setSearchClass(Course.class);
		courseList = courseService.getObjectTotalList(searchContext);
		return SUCCESS;
	}
	
	@Action(value = "addcourse", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String saveCourse() {
		major = majorService.getObjById(objId);
		SearchContext searchContext = new SearchContext();
		if(courseIdList==null){
			
		}else{
			searchContext.addOption(new SearchOption("id",courseIdList.toString().replace("[", "").replace("]", ""),SearchOption.Option.in));
			searchContext.setSearchClass(Course.class);
			courseList = courseService.getObjectTotalList(searchContext);
		}
		major.setCourseList(courseList);
		majorService.update(major);
		return SUCCESS;
	}

	@Action(value = "toedit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majors/major_edit.ftl") })
	public String edit() {
		SearchContext searchContext = new SearchContext();
		searchContext.setSearchClass(Majortype.class);
		majortypeList = majortypeService.getObjectTotalList(searchContext);
		major = majorService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String update() {
		Major temp = majorService.getObjById(major.getId());
		major.setCreatedTime(temp.getCreatedTime());
		major.setModifiedTime(new Date());
		BeanUtils.copyProperties(temp, major);
		majorService.update(temp);
		major = temp;
		return SUCCESS;
	}
	
	@Action(value = "toview", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majors/major_view.ftl") })
	public String view() {
		major = majorService.getObjById(objId);
		return SUCCESS;
	}
	
	@Action(value = "toviewcourse", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majors/major_view_course.ftl") })
	public String viewCourse() {
		major = majorService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majors/major_list.ftl") })
	public String list() {
		page = PageUtil.createPage(pageSize, currentPage, majorService
				.getObjectCount(getListSearchContext()).intValue());
		majorList = majorService.getObjectList(getListSearchContext(), pageSize,
				currentPage);
		return SUCCESS;
	}

	@Action(value = "del", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String del() {
		Major temp = majorService.getObjById(objId);
		majorService.delete(temp);
		return SUCCESS;
	}
	
	public MajorService getMajorService() {
		return majorService;
	}

	public void setMajorService(MajorService majorService) {
		this.majorService = majorService;
	}

	public Major getMajor() {
		return major;
	}

	public void setMajor(Major major) {
		this.major = major;
	}

	public List<Major> getMajorList() {
		return majorList;
	}

	public void setMajorList(List<Major> majorList) {
		this.majorList = majorList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}

	public String getMajorName() {
		return majorName;
	}

	public void setMajorName(String majorName) {
		this.majorName = majorName;
	}

	public String getMajorTerms() {
		return majorTerms;
	}

	public void setMajorTerms(String majorTerms) {
		this.majorTerms = majorTerms;
	}

	public String getTuitionRange() {
		return tuitionRange;
	}

	public void setTuitionRange(String tuitionRange) {
		this.tuitionRange = tuitionRange;
	}

	public String getMajorFields() {
		return majorFields;
	}

	public void setMajorFields(String majorFields) {
		this.majorFields = majorFields;
	}

	public String getMajorDescription() {
		return majorDescription;
	}

	public void setMajorDescription(String majorDescription) {
		this.majorDescription = majorDescription;
	}

	public String getStartEnrollmentTime() {
		return startEnrollmentTime;
	}

	public void setStartEnrollmentTime(String startEnrollmentTime) {
		this.startEnrollmentTime = startEnrollmentTime;
	}

	public String getEndEnrollmentTime() {
		return endEnrollmentTime;
	}

	public void setEndEnrollmentTime(String endEnrollmentTime) {
		this.endEnrollmentTime = endEnrollmentTime;
	}

	public List<Majortype> getMajortypeList() {
		return majortypeList;
	}

	public void setMajortypeList(List<Majortype> majortypeList) {
		this.majortypeList = majortypeList;
	}

	public MajortypeService getMajortypeService() {
		return majortypeService;
	}

	public void setMajortypeService(MajortypeService majortypeService) {
		this.majortypeService = majortypeService;
	}

	public CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public List<Course> getCourseList() {
		return courseList;
	}

	public void setCourseList(List<Course> courseList) {
		this.courseList = courseList;
	}

	public List<Integer> getCourseIdList() {
		return courseIdList;
	}

	public void setCourseIdList(List<Integer> courseIdList) {
		this.courseIdList = courseIdList;
	}

}
